<?php 
$page_title = "21 Game - Game";
include("header.php"); ?>

<!-- page header -->
<div class="page-header">
	<div class="container">
		<h1>21 Game</h1>
	</div>
</div>
<!-- end page header -->

<div class="container">

<?php
if(isset($_GET['gameid']) and isset($_GET['player1']) and isset($_GET['player2'])){
	$gameid = $_GET['gameid'];
	$player1 = $_GET['player1'];
	$player2 = $_GET['player2'];

require("sql/connection.php");
//We check if the game exists, if not we don't show the game.
$query = "SELECT Game_ID, Player1, Player2, Turn FROM Games WHERE Game_ID = '$gameid' AND Player1 = '$player1'
AND Player2 = '$player2' AND Status = 'Accepted'";
$r = mysqli_query($dbc,$query);
$row = mysqli_fetch_array($r,MYSQLI_ASSOC);

if(mysqli_num_rows($r) == 1){
	//Check if its our turn to play
	if($row['Turn'] == $_SESSION['username']){
		echo "<div class='container'>";
		echo "<div class='col-lg-8'>";
		if($_POST['action'] == "Give me another number"){
			$score = $_POST['score'];
			$score = $score + rand(1,12);
			echo "<p class='lead'>$score</p>";
		}
		else if($_POST['action'] == "Stop"){
			$hideform=1;
			$score = $_POST['score'];
			if($row['Player2'] == $_SESSION['username']){
				$query = "UPDATE Games SET Score_Player2 = '$score', Turn = '$player1' WHERE Game_ID = $gameid";
				$r = mysqli_query($dbc,$query);
				echo "<p class='lead'>Your score has been sent.</p>";
				header('Refresh: 3; url=index.php');
			}
			else{
				$query = "UPDATE Games SET Score_Player1 = '$score', Turn = 'NULL', Status = 'Finished' WHERE Game_ID = $gameid";
				$r = mysqli_query($dbc,$query);
				
				//Check who is the winner
				$query = "SELECT Score_Player1, Score_Player2 FROM Games WHERE Game_ID = $gameid";
				$r = mysqli_query($dbc,$query);
				$row = mysqli_fetch_array($r,MYSQLI_ASSOC);

				$scorep1 = $row['Score_Player1'];
				$scorep2 = $row['Score_Player2'];

				if($scorep1 > $scorep2){
					$query = "UPDATE Games SET Winner = '$player1' WHERE Game_ID = '$gameid'";
					$r = mysqli_query($dbc,$query);

					$query = "UPDATE Users SET Victories = Victories + 1 WHERE Nickname = '$player1'";
					$r = mysqli_query($dbc,$query);
					$query = "UPDATE Users SET Loses = Loses + 1 WHERE Nickname = '$player2'";
					$r = mysqli_query($dbc,$query);
					echo "<p class='lead'>$player1 wins the game with <b>$scorep1</b>.</p>";
					echo "<a href='yourgames.php'>Back</a>";
				}
				else{
					$query = "UPDATE Games SET Winner = '$player2' WHERE Game_ID = '$gameid'";
					$r = mysqli_query($dbc,$query);

					$query = "UPDATE Users SET Victories = Victories + 1 WHERE Nickname = '$player2'";
					$r = mysqli_query($dbc,$query);
					$query = "UPDATE Users SET Loses = Loses + 1 WHERE Nickname = '$player1'";
					$r = mysqli_query($dbc,$query);
					echo "<p class='lead'>$player2 wins the game with <b>$scorep2</b>.</p>";
					echo "<a href='yourgames.php'>Back</a>";
				}
			}
		}
		echo "</div>";
		?>
		<div class="col-lg-4">
		<form action="" method="post" role="form">
			<?php 
				if(!isset($hideform)){
					echo '<input type="submit" class="btn btn-primary" name="action" value="Give me another number"><br>';
					if(isset($score)){
					echo '<input type="submit" class="btn btn-primary" name="action" value="Stop">';
				}
			}
			?>
			<input type="hidden" name="score" value=<?php echo $score; ?>>
		</form>
		</div>
		<?php
		echo "</div>";
	}
	else{
		echo "It's your opponent turn.";
	}
}
else{
	echo "This game already finished or doesn't exist.";
}

}
else{
	header("Location: yourgames.php");
}

 ?>
</div>

<?php include("footer.php"); ?>